package com.netsdk.lht2.handle.support;

import com.netsdk.demo.util.DateUtil;
import com.netsdk.demo.util.sql.EventDto;
import com.netsdk.lht2.handle.EventHandle;
import com.netsdk.lib.NetSDKLib;
import com.netsdk.lib.ToolKits;
import com.netsdk.lib.structure.DEV_EVENT_DREGS_UNCOVERED_INFO;
import com.netsdk.lib.structure.DREGS_UNCOVERED_VEHICLE_INFO;
import com.netsdk.lib.structure.NET_TIME_EX;
import com.sun.jna.Pointer;

import java.io.UnsupportedEncodingException;
import java.util.Date;

import static com.netsdk.lib.NetSDKLib.NET_MAX_DETECT_REGION_NUM;

/*
* 停车检测处理
* */
public class EVENT_IVS_TRAFFIC_PARKING_HANDLE extends EventHandle {
    @Override
    public void handle(int dwAlarmType, Pointer pAlarmInfo, Pointer pBuffer, int dwBufSize, Pointer dwUser, int nSequence, Pointer reserved) throws UnsupportedEncodingException {
        EventDto dto = new EventDto();
        dto.setEvent_name("停车检测");
        dto.setEvent_code(dwAlarmType+"");
        DEV_EVENT_DREGS_UNCOVERED_INFO msg = new DEV_EVENT_DREGS_UNCOVERED_INFO();

        ToolKits.GetPointerData(pAlarmInfo, msg);
        // 通道号
        String nChannelID = getChannelMapping(msg.nChannelID);
        System.out.println("nChannelID:" + msg.nChannelID);
        dto.setChannel(nChannelID);
        //事件名称
        byte[] szName = msg.szName;
        dto.setSzName( new String(msg.szName,"UTF-8").trim());
        //相对事件时间戳(单位是毫秒)
        double pts = msg.PTS;
        dto.setEvent_time(DateUtil.format(new Date((long) pts), "yyyy-MM-dd HH:mm:ss.SSS"));
        //UTC时间
        NET_TIME_EX UTC = msg.UTC;
        System.out.println("UTC:" + UTC.toStringTime());
        dto.setEvent_utc_time(UTC.toStringTime());
        //事件id
        int nEventID = msg.nEventID;
        dto.setEvent_id(nEventID+"");
        //智能事件所属大类
        int ClassType = msg.emClassType;
        dto.setEvent_class(ClassType+"");
        //检测区域,[0,8191]
        int nDetectRegionNum = msg.nDetectRegionNum;
        dto.setRegion(nDetectRegionNum+"");
        //事件对应文件信息
        String stuDetectRegions = "";
        NetSDKLib.NET_POINT[] stuDetectRegion = msg.stuDetectRegion;
        for(int i = 0; i < NET_MAX_DETECT_REGION_NUM; i ++){
            // System.out.println("stuDetectRegion[" + i + "]:" + stuDetectRegion[i].toString());
            stuDetectRegions = stuDetectRegions + stuDetectRegion[i].toString()+",";
        }
        dto.setEvent_code(stuDetectRegions);
        //车辆信息
        DREGS_UNCOVERED_VEHICLE_INFO stuVehicleInfo = msg.stuVehicleInfo;

        dto.setStu_Vehicle_Info(new String(stuVehicleInfo.szPlateNumber).trim());
        //图片保存
        if (pBuffer != null) {
            String pName = System.currentTimeMillis() + "related_EVENT_IVS_DREGS_UNCOVERED.jpg";
            String picture = picturePath + "/" + pName;
            if(null != pBuffer){
                dto.setImg_name(pName);
            }
            ToolKits.savePicture(pBuffer, 0, dwBufSize, picture);
            System.out.println("图片:" + picture);
        }
        // 数据入库
        insertEventDto(dto);
    }
}
